package com.atguigu.daijia.driver.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.dispatch.client.NewOrderFeignClient;
import com.atguigu.daijia.driver.client.DriverInfoFeignClient;
import com.atguigu.daijia.driver.service.DriverService;
import com.atguigu.daijia.map.client.LocationFeignClient;
import com.atguigu.daijia.model.entity.driver.DriverSet;
import com.atguigu.daijia.model.form.driver.DriverFaceModelForm;
import com.atguigu.daijia.model.form.driver.UpdateDriverAuthInfoForm;
import com.atguigu.daijia.model.vo.driver.DriverAuthInfoVo;
import com.atguigu.daijia.model.vo.driver.DriverLoginVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.lang.management.OperatingSystemMXBean;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class DriverServiceImpl implements DriverService {
    @Resource
    private DriverInfoFeignClient driverInfoFeignClient;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private LocationFeignClient locationFeignClient;
    @Resource
    private NewOrderFeignClient newOrderFeignClient;

    @Override
    public String login(String code) {
        //获取id
        Result<Long> driverId = driverInfoFeignClient.login(code);
        //String driverIdString = (String)driverId;

        //静id和token存入redis
        String token = UUID.randomUUID().toString().replaceAll("-","");
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX+token,driverId.getData().toString(),30, TimeUnit.MINUTES);
        //返回token
        return token;
    }

//    @Override
//    public DriverLoginVo getDriverLoginInfo(Long driverId) {
//        Result<DriverLoginVo> driverLoginInfo = driverInfoFeignClient.getDriverLoginInfo(driverId);
//        return driverLoginInfo.getData();
//    }

    @Override
    public DriverAuthInfoVo getDriverAuthInfo(Long id) {
        return driverInfoFeignClient.getDriverAuthInfo(id).getData();
    }

    @Override
    public Boolean updateDriverAuthInfo(UpdateDriverAuthInfoForm updateDriverAuthInfoForm) {

        return driverInfoFeignClient.UpdateDriverAuthInfo(updateDriverAuthInfoForm).getData();
    }

    @Override
    public Boolean creatDriverFaceModel(DriverFaceModelForm driverFaceModelForm) {
        return driverInfoFeignClient.creatDriverFaceModel(driverFaceModelForm).getData();
    }
    @Override
    public Boolean isFaceRecognition(Long driverId) {
        return driverInfoFeignClient.isFaceRecognition(driverId).getData();
    }

    //人脸识别
    @Override
    public Boolean verifyDriverFace(DriverFaceModelForm driverFaceModelForm) {
        return driverInfoFeignClient.verifyDriverFace(driverFaceModelForm).getData();
    }

    @Override
    public Boolean startService(Long driverId) {
        //开始接单前要检查司机是否开启了实名认证，人脸认证之类的服务，这也是在这些接口没有被直接调用的原因。
        Result<DriverLoginVo> driverLoginInfo = driverInfoFeignClient.getDriverLoginInfo(driverId);
        DriverLoginVo infoData = driverLoginInfo.getData();
        //是否实名认证
        if(infoData.getAuthStatus() != 2){
            throw new GuiguException(ResultCodeEnum.AUTH_ERROR);
        }
        Boolean isFace = driverInfoFeignClient.isFaceRecognition(driverId).getData();
        //是否进行每日人脸认证
        if(!isFace){
            throw new GuiguException(ResultCodeEnum.FACE_ERROR);
        }

        //3 更新订单状态 1 开始接单
        driverInfoFeignClient.updateServiceStatus(driverId,1);
        //既然司机开始接单自然要删除在redis中的位置信息
        Boolean booleanLocation =  locationFeignClient.removeDriverLocation(driverId).getData();
        if(!booleanLocation){
            throw new GuiguException(ResultCodeEnum.FAIL);
        }
        //清空司机临时队列的消息
        Boolean data = newOrderFeignClient.clearNewOrderQueueData(driverId).getData();
        if(!data){
            throw new GuiguException(ResultCodeEnum.FAIL);
        }

        return true;
    }

    @Override
    public Boolean stopService(Long driverId) {
        Boolean booleanLocation =  locationFeignClient.removeDriverLocation(driverId).getData();
        if(!booleanLocation){
            throw new GuiguException(ResultCodeEnum.FAIL);
        }
        //清空司机临时队列的消息
        Boolean data = newOrderFeignClient.clearNewOrderQueueData(driverId).getData();
        if(!data){
            throw new GuiguException(ResultCodeEnum.FAIL);
        }
        // 更新订单状态  停止接单
        driverInfoFeignClient.updateServiceStatus(driverId,2);
        return true;

    }
}
